"""
-*- coding:utf-8 -*-
最终版web自动化
Author: Free
DataTime: 2020/7/1 18:29

"""

from WebSite.tools.ReadConfig import ReadConfig
import unittest
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import warnings


class TaskPublic(unittest.TestCase):

    def setUp(self):
        warnings.simplefilter("ignore", ResourceWarning)
        self.b = webdriver.Remote(
            command_executor='http://127.0.0.1:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.CHROME)

        url = ReadConfig.url_first.value
        self.b.get(url)
        self.b.maximize_window()
        self.b.implicitly_wait(10)

    def codeBi(self):
        """
        登录验证码滑动
        # TODO: 使用滑动验证码破解时，不能点击第二个登录（click_login）
        :return:
        """
        for i in range(100):
            self.click_login()
            sleep(2)
            element = self.b.find_element_by_css_selector('#mpanel1 > div.verify-bar-area > div > div')

            print("开始进行滑动")
            ActionChains(self.b).click_and_hold(on_element=element).perform()
            ActionChains(self.b).move_to_element_with_offset(to_element=element, xoffset=43,
                                                             yoffset=0).perform()
            sleep(0.8)
            ActionChains(self.b).release().perform()
            sleep(1)
            print("滑动重试%d次数" % (i + 1))
            if self.b.current_url == ReadConfig.url_login.value:
                self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div.bigBox > div:nth-child(2) > div.slideBarBigBoxW > div > div.slideBarBoxX > img').click()
                continue
            else:
                sleep(2)
                print("正确滑动")
                break

    def login_pc(self):
        """
        定位登陆按钮
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div.homeBannerAndHeadBox > div.homeHeadBox > div > div > ul > li:nth-child(1) > div.loginBtn').click()
            sleep(2)
            print("定位第一个登陆成功")
        except Exception as e:
            print('定位第一个登陆失败', e)

    def input_text(self, user=ReadConfig.user.value):
        """
        默认取配置
        :param user: 用户名可修改、
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div.bigBox > div:nth-child(2) > div:nth-child(4) > div > input').send_keys(user)
            sleep(2)
            print("输入用户名成功")
        except Exception as e:
            print('输入用户名失败', e)

    def input_password(self, password=ReadConfig.password.value):
        """
        默认取配置
        :param password: 密码可修改
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div.bigBox > div:nth-child(2) > div:nth-child(5) > div > input').send_keys(password)
            sleep(2)
            print('输入密码成功')
        except Exception as e:
            print('输入密码失败', e)

    def click_login(self):
        """
        进行登录按钮(登录后的下面的登录按钮)
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div.bigBox > div:nth-child(2) > div.loginButtonBox > button').click()
            sleep(2)
            print('进行登录按钮(登录后的下面的登录按钮)成功')
        except Exception as e:
            print('进行登录按钮(登录后的下面的登录按钮)失败', e)

    def tearDown(self) -> None:
        self.b.close()
        self.b.quit()
